﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LeetCodeSolutions
{
    /// <summary>
    /// LeetCode题库2 两数相加 https://leetcode-cn.com/problems/add-two-numbers/
    /// </summary>
    class LeetCode2: LeetCodeBase
    {
        public override void Execute()
        {
            ListNode l1 = new ListNode(2);
            l1.next = new ListNode(4);
            l1.next.next = new ListNode(3);

            ListNode l2 = new ListNode(5);
            l2.next = new ListNode(6);
            l2.next.next = new ListNode(4);

            ListNode res = AddTwoNumbers(l1, l2);
            res.toString(res);
        }

        public class ListNode
        {
            public int val;
            public ListNode next;
            public ListNode(int val = 0, ListNode next = null)
            {
                this.val = val;
                this.next = next;
            }

            public void toString(ListNode node)
            {
                string log = "";
                while (node != null)
                {
                    log += node.val + " ";
                    node = node.next;
                }

                Console.Write("输出：" + log);
            }
        }

        public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
        {
            ListNode headNode = new ListNode(0);
            ListNode lastNode = headNode;
            int val = 0;
            while (l1 != null || l2 != null || val != 0)
            {
                val = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + val;
                lastNode.next = new ListNode(val % 10);
                lastNode = lastNode.next;
                val /= 10;
                l1 = l1 == null ? null : l1.next;
                l2 = l2 == null ? null : l2.next;
            }

            return headNode.next;

            /*ListNode resultNode = new ListNode();
            int[] nums1 = new int[0];
            int[] nums2 = new int[0];
            int node1Value = CountList(l1, ref nums1);
            int node2Value = CountList(l2, ref nums2);

            int resultValue = node1Value + node2Value;

            return resultNode;*/
        }

        public ListNode ReverseToListNode(int value, int[] nums)
        {
            GetLength(value);
            int length = count;
            for (int i = length; i > 0; i--)
            {
                ListNode node = new ListNode(nums[i]);
                AddListNode(node);
            }

            return pNode;
        }

        ListNode pNode = new ListNode();
        int nodeCount = 0;
        private void AddListNode(ListNode node)
        {
            if (nodeCount == 0)
            {
                pNode = node;
            }
            else
            {
                pNode.next = node;
            }
        }

        int count = 0;
        //获取数字的位数
        private void GetLength(int value)
        {
            int num = value & 10;
            if (num != 0)
            {
                count++;
                GetLength(num);
            }
        }

        //计算node的逆向和
        public int CountList(ListNode node, ref int[] indexes)
        {
            int length = 0;
            while (node.next != null)
            {
                length++;
                indexes = new int[length];
                indexes[length - 1] = node.val;
                node = node.next;
            }

            int result = 0;
            for (int i = length - 1; i > 0; i--)
            {
                int pow = indexes[i] * Pow(i);
                result += pow;
            }

            return result;
        }

        public int Pow(int i)
        {
            if (i > 0)
            {
                return Pow(--i);
            }

            return 0;
        }
    }
}
